package com.wechat.service;

import com.wechat.entity.Question;
import com.wechat.entity.User;
import com.wechat.entity.Answer;
import com.wechat.mapper.QuestionMapper;
import com.wechat.mapper.UserMapper;
import com.wechat.mapper.AnswerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;
import java.sql.Timestamp;

@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AnswerMapper answerMapper;

    public List<Map<String, Object>> listQuestions() {
        List<Question> questions = questionMapper.selectList(new QueryWrapper<Question>().orderByDesc("created_at"));
        List<Map<String, Object>> dtos = new ArrayList<>();
        for (Question q : questions) {
            Map<String, Object> dto = new HashMap<>();
            dto.put("id", q.getId());
            dto.put("title", q.getTitle());
            dto.put("summary", q.getContent().length() > 50 ? q.getContent().substring(0, 50) + "..." : q.getContent());
            User user = userMapper.selectById(q.getUserId());
            dto.put("author", user != null ? user.getUsername() : "未知");
            dto.put("createdAt", q.getCreatedAt());
            dto.put("viewCount", q.getViewCount());
            dto.put("answerCount", q.getAnswerCount());
            dtos.add(dto);
        }
        return dtos;
    }

    public Map<String, Object> getQuestionDetail(Long id) {
        Question q = questionMapper.selectById(id);
        if (q == null)
            return null;
        // 阅读数自增
        q.setViewCount(q.getViewCount() + 1);
        questionMapper.updateById(q);
        User user = userMapper.selectById(q.getUserId());
        Map<String, Object> questionDTO = new HashMap<>();
        questionDTO.put("id", q.getId());
        questionDTO.put("title", q.getTitle());
        questionDTO.put("content", q.getContent());
        questionDTO.put("author", user != null ? user.getUsername() : "未知");
        questionDTO.put("createdAt", q.getCreatedAt());
        questionDTO.put("viewCount", q.getViewCount());
        questionDTO.put("answerCount", q.getAnswerCount());
        // 回答列表
        List<Answer> answers = answerMapper
                .selectList(new QueryWrapper<Answer>().eq("question_id", id).orderByAsc("created_at"));
        List<Map<String, Object>> answerDTOs = new ArrayList<>();
        for (Answer a : answers) {
            Map<String, Object> adto = new HashMap<>();
            adto.put("id", a.getId());
            adto.put("content", a.getContent());
            User au = userMapper.selectById(a.getUserId());
            adto.put("author", au != null ? au.getUsername() : "未知");
            adto.put("createdAt", a.getCreatedAt());
            answerDTOs.add(adto);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("question", questionDTO);
        result.put("answers", answerDTOs);
        return result;
    }

    public void addAnswer(Long questionId, String content, Long userId) {
        Answer answer = new Answer();
        answer.setQuestionId(questionId);
        answer.setContent(content);
        answer.setUserId(userId);
        answer.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        answerMapper.insert(answer);
        // 回答数+1
        Question q = questionMapper.selectById(questionId);
        q.setAnswerCount(q.getAnswerCount() + 1);
        questionMapper.updateById(q);
    }

    public void addQuestion(Question question) {
        questionMapper.insert(question);
    }
}